package wh.回文串;

import wh.实现strStr函数.Str;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2022/4/5 9:11
 * @description:如果在将所有大写字符转换为小写字符、并移除所有非字母数字字符之后，短语正着读和反着读都一样。则可以认为该短语是一个 回文串
 *
 * @version:
 */
public class Solution {
    public static void main(String[] args) {
        System.out.println(isPalindrome2("0P0"));
    }

    //解法1 字符提取
    public static boolean isPalindrome(String s) {
        String s1 = s.toLowerCase();
        char[] chars = s1.toCharArray();
        List<Character> list = new ArrayList<>();
        for (int i = 0; i < chars.length; i++) {
            //char转ascii
            if((chars[i] + 0 >= 97 && chars[i] + 0 <= 122) || (chars[i] + 0 >= 48 && chars[i] + 0 <= 57)){
                list.add(chars[i]);
            }
        }
        if(list.isEmpty()){
            return true;
        }
        char[] newChar = new char[list.size()];
        for (int i = 0; i < list.size(); i++) {
            newChar[i] = list.get(i);
        }
        int n = 0;
        int m = newChar.length - 1;
        while (n < m){
            if(newChar[n] != newChar[m]){
                return false;
            }else {
                n++;
                m--;
            }
        }
        return true;
    }

    //解法2 字符替换
    public static boolean isPalindrome2(String s){
        String s1 = s.toLowerCase();
        for (int i = 0; i < s1.length(); i++) {
            if(!((s1.charAt(i) + 0 >= 97 && s1.charAt(i) + 0 <= 122) || (s1.charAt(i) + 0 >= 48 && s1.charAt(i) + 0 <= 57))){
                s1 = s1.replace(s1.charAt(i), ' ');
            }
        }
        String replace = s1.replace(" ", "");
        int i = 0;
        int j = replace.length() - 1;
        if(j < 0){
            return true;
        }
        while (i < j){
            if(replace.charAt(i) != replace.charAt(j)){
                return false;
            }else {
                i++;
                j--;
            }
        }
        return true;
    }

    //解法3  StringBuffer的api
    public static boolean isPalindrome3(String s) {
        StringBuffer sgood = new StringBuffer();
        int length = s.length();
        for (int i = 0; i < length; i++) {
            char ch = s.charAt(i);
            //筛选字母和数字
            if (Character.isLetterOrDigit(ch)) {
                sgood.append(Character.toLowerCase(ch));
            }
        }
        //反转
        StringBuffer sgood_rev = new StringBuffer(sgood).reverse();
        return sgood.toString().equals(sgood_rev.toString());
    }


    //解法4 双指针
    public static boolean isPalindrome4(String s) {
        StringBuffer sgood = new StringBuffer();
        int length = s.length();
        //移除非字母数字
        for (int i = 0; i < length; i++) {
            char ch = s.charAt(i);
            if (Character.isLetterOrDigit(ch)) {
                sgood.append(Character.toLowerCase(ch));
            }
        }
        int n = sgood.length();
        int left = 0, right = n - 1;
        while (left < right) {
            if (Character.toLowerCase(sgood.charAt(left)) != Character.toLowerCase(sgood.charAt(right))) {
                return false;
            }
            ++left;
            --right;
        }
        return true;
    }




}
